﻿using System;
using CoreTypes;
using System.Custom;
using System.Drawing;
using System.Windows.Forms;
using MainModule.Properties;
using System.ComponentModel;

namespace MainModule
{
   public partial class ProfileManager : UserControl
   {
      InternetCommunicator FCommunicator;

      public ProfileManager()
      {
         InitializeComponent();

         imageList.Images.Add(Resources.UserGreen);
      }

      internal void Initialize()
      {
         FCommunicator = InternetCommunicator.Self;

         FCommunicator.AccountAdded += communicator_AccountAdded;
         FCommunicator.ProfileLoaded += communicator_ProfileLoaded;
         FCommunicator.StatusChanged += communicator_StatusChanged;
         FCommunicator.AccountRemoved += communicator_AccountRemoved;
         FCommunicator.PropertyChanged += communicator_PropertyChanged;
         FCommunicator.SaveAccountDataState += communicator_SaveAccountDataState;

         RefreshView();
      }
      private void RefreshView()
      {
         textBoxPath.Text = FCommunicator.AppDataDir;
         itemMyDocuments.Checked = FCommunicator.AppDataDir.StartsWith(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
         itemProgramFiles.Checked = FCommunicator.AppDataDir.StartsWith(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
         itemApplicationData.Checked = FCommunicator.AppDataDir.StartsWith(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));

         listViewAccounts.Items.Clear();

         foreach (Account account in FCommunicator.Accounts) AddAccount(account, false);
      }
      private void UpdateControls()
      {
         Account account = GetSelectedAccount();

         if (account == null)
         {
            buttonTurnOnOffAccount.Text = Resources.S_On_Off;
         }
         else
         {
            buttonTurnOnOffAccount.Text = FCommunicator.ActiveAccounts.Contains(account) ? Resources.S_Turn_off : Resources.S_Turn_on;
         }
      }
      private Account GetSelectedAccount()
      {
         if (listViewAccounts.SelectedItems.Count > 0)
         {
            return (Account)listViewAccounts.SelectedItems[0].Tag;
         }
         return null;
      }
      private ListViewItem GetListViewItem(Account account)
      {
         foreach (ListViewItem item in listViewAccounts.Items)
         {
            if (item.Tag == account) return item;
         }
         return null;
      }
      private void AddAccount(Account account, bool activated)
      {
         if (activated)
         {
            ListViewItem item = GetListViewItem(account);
            if (item != null)
            {
               item.SubItems[2].Text = Resources.S_Yes;
               item.SubItems[3].Text = account.AutoLogon ? Resources.S_Yes : Resources.S_No;
            }
         }
         else
         {
            ListViewItem item = new ListViewItem(account.ToString(InfoStrings.NameServiceStatus), 0);

            item.Tag = account;

            item.SubItems.AddRange(new string[]
               { account.ServiceDriver == null ? Resources.S_Transport_not_set : account.ServiceConfig.TransportString,
                 FCommunicator.ActiveAccounts.Contains(account) ? Resources.S_Yes : Resources.S_No,
                 account.AutoLogon ? Resources.S_Yes : Resources.S_No
               });

            listViewAccounts.Items.Add(item);
         }
      }
      private void RemoveAccount(Account account, bool unload)
      {
         ListViewItem item = GetListViewItem(account);
         if (item != null)
         {
            if (unload) item.SubItems[2].Text = Resources.S_No;
            else listViewAccounts.Items.Remove(item);
         }
      }

      private void itemNavigate_Click(object sender, EventArgs e)
      {
         Shell.RunProcess(textBoxPath.Text, "", true);
      }
      private void itemSelectFolder_Click(object sender, EventArgs e)
      {
         string path = "";

         if (sender == itemMyDocuments) path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
         else if (sender == itemProgramFiles) path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
         else if (sender == itemApplicationData) path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
         else if (folderBrowserDialog.ShowDialog() == DialogResult.OK) path = folderBrowserDialog.SelectedPath;

         if (path != "") FCommunicator.SetDataDir(path);
      }
      private void buttonAddAccount_Click(object sender, EventArgs e)
      {
         FindForm().Close();
         FCommunicator.ShowAddAccount();
      }
      private void buttonRemoveAccount_Click(object sender, EventArgs e)
      {
         Account account = GetSelectedAccount();
         if (account == null) return;
         
         FindForm().Close();
         FCommunicator.ShowRemoveAccount(account, false);
      }
      private void buttonExportProfile_Click(object sender, EventArgs e)
      {
         FCommunicator.ExportProfile(null);
      }
      private void buttonImportProfile_Click(object sender, EventArgs e)
      {
         FCommunicator.ImportProfile(null);
      }
      private void communicator_ProfileLoaded(object sender, EventArgs e)
      {
         RefreshView();
         UpdateControls();
      }
      private void buttonTurnOnOffAccount_Click(object sender, EventArgs e)
      {
         Account account = GetSelectedAccount();

         if (account != null)
         {
            if (FCommunicator.ActiveAccounts.Contains(account))
            {
               if (!FCommunicator.DeactivateAccount(account))
               {
                  MessageBox.Show(Resources.S_You_cannot_unload_the_only_active_account, Resources.S_Info, MessageBoxButtons.OK, MessageBoxIcon.Information);
               }
            }
            else
            {
               FCommunicator.CurrentAccount = account;
            }
         }
      }
      private void buttonAccountProperties_Click(object sender, EventArgs e)
      {
         Account account = GetSelectedAccount();
         if (account != null) FCommunicator.ShowUserProperties(account, null);
      }
      private void buttonNetworkProperties_Click(object sender, EventArgs e)
      {
         Account account = GetSelectedAccount();
         if (account != null) account.ServiceConfig.Service.Driver.ShowDriverSettings(account.ServiceConfig.ToString());
      }
      private void buttonStorageDirectory_MouseUp(object sender, MouseEventArgs e)
      {
         menuProfileDirectory.Show(buttonStorageDirectory, new Point(0, buttonStorageDirectory.Height));
      }
      private void listViewAccounts_SelectedIndexChanged(object sender, EventArgs e)
      {
         UpdateControls();
      }
      private void communicator_AccountAdded(object sender, AccountAddedEventArgs e)
      {
         AddAccount(e.Account, e.Activated);
         UpdateControls();
      }
      private void communicator_StatusChanged(object sender, StatusChangedEventArgs e)
      {
         RefreshView();
      }
      private void communicator_AccountRemoved(object sender, AccountRemovedEventArgs e)
      {
         RemoveAccount(e.Account, e.Deactivated);
         UpdateControls();
      }
      private void communicator_PropertyChanged(object sender, PropertyChangedEventArgs e)
      {
         if (e.PropertyName == "AppDataDir") RefreshView();
      }
      private void communicator_SaveAccountDataState(object sender, NetworkOperationEventArgs e)
      {
         RefreshView();
      }
  }
}